#pragma once
#include <iostream>
#include <queue>
template<class T>
class BlockQueue
{
    const static int defaultnumber=5;
public:
    BlockQueue(int maxcap=defaultnumber):maxcap_(maxcap)
    {
        pthread_mutex_init(&mutex_,nullptr);
        pthread_cond_init(&c_cond,nullptr);
        pthread_cond_init(&p_cond,nullptr);

    }

    ~BlockQueue()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&c_cond);
        pthread_cond_destroy(&p_cond);

    }
    void push(const T& date)
    {
        pthread_mutex_lock(&mutex_);
        if(q_.size()==maxcap_)
        {
            //pthread_cond_signal(&c_cond);
            pthread_cond_wait(&p_cond,&mutex_);
        }
        q_.push(date);
        pthread_mutex_unlock(&mutex_);
        pthread_cond_signal(&c_cond);
         std::cout<<"生产一个"<<std::endl;
    }
    

    T pop()
    {
        pthread_mutex_lock(&mutex_);
        if(q_.size()==0)
        {
            //pthread_cond_signal(&p_cond);
            pthread_cond_wait(&c_cond,&mutex_);
        }
        T out=q_.front();
        q_.pop();
        pthread_mutex_unlock(&mutex_);
        pthread_cond_signal(&p_cond);
        std::cout<<"消费一个"<<std::endl;
        return out;
    }


private:
    std::queue<T> q_;
    int maxcap_;
    pthread_mutex_t mutex_;
    pthread_cond_t c_cond;
    pthread_cond_t p_cond;
};